﻿using pbvs.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;

namespace pbvs.Utilities
{
    public class Validation
    {
        public static bool ValuationRequest(ValuationRequest req)
        {
            bool isValid = true;
            List<string> values = new List<string>{ req.ParticipantFirstName, req.ParticipantLastName };
            isValid = ValidStrings(values) && ValidRadio(req.Gender, new List<string> { "M", "F" }) && ValidPastDate(req.Dob) & ValidPastDate(req.Doh) && ValidConditionalDecimal(req.OverrideInterest, req.InterestRate) && ValidConditionalDecimal(req.SubjectToCola, req.Cola);
            if (req.CalculateMarriage)
            {
                isValid = ValidConditionalDecimal(!req.UseFractional, req.MaritalPortion) && ValidPastDate(req.MarriageDate) && ValidPastDate(req.MarriageEarningEndDate);
            }
            if (req.Survivorship)
            {
                values = new List<string> { req.SurvivorFirstName, req.SurvivorLastName };
                isValid = ValidStrings(values) && ValidRadio(req.SurvivorGender, new List<string> { "M", "F" }) && ValidPastDate(req.SurvivorDob);
            }
            return isValid;
        }

        public static bool ValidConditionalDecimal(bool conditional, decimal value)
        {
            bool isValid = true;
            if (conditional)
            {
                isValid = value > 0;
            }
            return isValid;
        }

        public static bool ValidRadio(string value, List<string> acceptableValues)
        {
            return acceptableValues.Contains(value);
        }

        public static bool ValidStrings<T>(T item)
        {
            var props = typeof(T).GetProperties().Where(p => p.PropertyType == typeof(string)).ToList();
            IList<string> values = new List<string>();
            foreach (PropertyInfo prop in props)
            {
                values.Add((string)prop.GetValue(item, null));
            }
            return ValidStrings(values);
        }

        public static bool ValidStrings(IEnumerable<string> values)
        {
            bool isValid = true;
            foreach(string val in values)
            {
                isValid = ValidString(val);
            }
            return isValid;
        }

        public static bool Client(Client client)
        {
            return ValidString(client.FirstName) && ValidString(client.LastName) && ValidEmail(client.Email);
        }

        public static bool ValidString(string value)
        {
            return !string.IsNullOrEmpty(value);
        }

        public static bool ValidEmail(string value)
        {
			Regex regex = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
            return regex.IsMatch(value);
        }

        public static bool ValidDate(string value)
        {
            DateTime dt;
            return DateTime.TryParse(value, out dt);
        }

        public static bool ValidPastDate(DateTime dt)
        {
            return DateTime.Now > dt;
        }
    }
}